package vn.edu.hcmut.cse.ai.search.problems.blocksworld;

import vn.edu.hcmut.cse.ai.search.local.HeuristicFunction;
import vn.edu.hcmut.cse.ai.search.problems.blocksworld.predicate.BlocksWorldPredicate;

public class BlocksWorldFirstHeuristicFunction implements HeuristicFunction{

    @Override
    public double heuristicEvaluation(Object currentState, Object goalState) {
        BlocksWorldState blocksWorldCurrentState = (BlocksWorldState) currentState;
        BlocksWorldState blocksWorldGoalState = (BlocksWorldState) goalState;
        
        // For each ON or ONTABLE predicate, check if it exists in the goal state
        // Plus one if it exists
        // Minus on if it does not exists
        int heuristicPoint = 0;
        for (BlocksWorldPredicate pred : blocksWorldCurrentState.getPredicates()) {
            if (pred.getPredicateType() == BlocksWorldPredicate.PRED_TYPE_ON ||
                    pred.getPredicateType() == BlocksWorldPredicate.PRED_TYPE_ONTABLE) {
                if (blocksWorldGoalState.getPredicates().contains(pred)) {
                    heuristicPoint ++;
                } else {
                    heuristicPoint --;
                }
            }
        }
        
        return heuristicPoint;
    }
    
}
